clear all
close all


path=['C:\Users\Tom�s\Desktop\Candelabrum Cell paper\Candelabrum Cells\Mossy Fiber input\11\']
firstFileNumber=539;
stimInt=20;
trials=10; 




%% load NBQX wash in and plot it
ntrials=100;
firstWashin=586;
for l= 1:ntrials; % ISI(1:end);
    
    
       
        IBW2(l).data= IBWread([path 'ch0_' num2str(firstWashin +l-1)  '.ibw' ]);
       IBW2Mat(:,l)=IBW2(l).data.y-mean(IBW2(l).data.y(1:1000));
end

%remove stim artifact
a=(abs(diff(mean(IBW2Mat(:,[80:100]),2))))>2;

IBW2Mat(a,:)=NaN;
IBW2medfilt=medfilt1(IBW2Mat,30);
time=[1:size(IBW2Mat,1)]*IBW2(1).data.dx*1000; % in ms


figure;plot(IBW2Mat(mean(50220-10:50220+10),:),'k.','markersize',20)
ylabel('I (pA)');
xlabel('trial (5s ITI)')

figure;plot(time,IBW2medfilt,'color' , [0 0 0 0.2],'linewidth',1)
xlim([ 498 510])
axis off
box off
set(gcf,'color','white')
obj=scalebar('XLen',1,'XUnit','ms','YLen',20,'YUnit','pA')



sumTrace=sum(IBW2Mat(50100:50650,:));

% figure;plot(sumTrace,'k.','markersize',20)

failure=sumTrace>-1000;
success=~failure;

color1=[199 34 22]./255;
fig2=figure(2);
fig2.Renderer='Painters';
plot(time,IBW2medfilt,'color' , [0 0 0 0.2],'linewidth',1)
xlim([ 498 510])
axis off
box off
set(gcf,'color','white')
obj=scalebar('XLen',1,'XUnit','ms','YLen',20,'YUnit','pA')
hold on
plot(time,mean(IBW2medfilt(:,success),2),'color',color1,'linewidth',2)
plot(time,mean(IBW2medfilt(:,failure),2),'k','linewidth',2)

%Make gaussian kernel histograms like we did for Velina's paper

tempe=IBW2Mat(mean(50220-10:50220+10),:);

v=violin(tempe');
v1=violin(tempe(success)');
v2=violin(tempe(failure)');
x0=(v.Vertices([1:100],1)-1).*numel(tempe);
x1=(v1.Vertices([1:100],1)-1).*sum(success);
x2=(v2.Vertices([1:100],1)-1).*sum(failure);
y0=v.Vertices([1:100],2);
y1=v1.Vertices([1:100],2);
y2=v2.Vertices([1:100],2);

color1=[199 34 22]./255;
color3=[0 0 0]
figure;
hold on
% area(y0,x0,'FaceColor',color3,'FaceAlpha',0.05,'EdgeColor',color3,'EdgeAlpha',0.05)
area(y1,x1,'FaceColor',color1,'FaceAlpha',0.5,'EdgeColor',color1,'EdgeAlpha',0.5)
area(y2,x2,'FaceColor',color3,'FaceAlpha',0.5,'EdgeColor',color3,'EdgeAlpha',0.5)
% plot(y0,x0,'color',[color3 0.4],'linewidth',2)
plot(y1,x1,'color',[color1 1],'linewidth',2)
plot(y2,x2,'color',[color3 1],'linewidth',2)
xlim([-90 10])
axis off
box off


% make histogram instead of violins
figure
histogram(tempe(success),'binwidth',3,'FaceColor',color1,'FaceAlpha',0.5,'EdgeColor',color1,'EdgeAlpha',0.5)
hold on
histogram(tempe(failure),'binwidth',3,'FaceColor',color3,'FaceAlpha',0.5,'EdgeColor',color3,'EdgeAlpha',0.5)





%% PPR
%manual enter of PPR data for now
ISI=[10 20 30 50 100];
MFPPR=[3.264725498	3.082458295	nan	1.39674271	1.194499225
0.884148213	1.252764733	0.826051269	0.881120917	0.759787061
1.165494975	1.049447978	0.877158901	0.863543726	0.952544528
1.771456	1.79489	1.033318	0.8723323	0.9689436
1.921464108	2.428216063	2.277185501	1.613006397	1.087775409
0.622	0.394	nan	0.6531	0.64
0.774	0.898	nan	0.71	0.79
1.0899	0.8263	nan	0.9504	0.542];

ISI2=[10 20 50 100 200 500];
PFPPR=[1.975123481	1.654692534	1.535657682	1.2092518	1.118658732	0.88231763
1.89980042	3.361853651	2.346426635	1.053554155	0.684517564	0.581373118
1.394036301	1.58150282	1.060474616	1.088685241	nan	0.868373691
1.249941938	1.151715898	1.168197398	1.067252018	0.997116344	0.972414906
1.878947697	1.731095054	1.432605858	1.059868217	0.724155198	1.211954067
1.734219203	1.736142102	1.644164579	1.316521127	1.323267035	0.897487815
1.449780394	1.626490286	1.470398194	1.112089368	nan	1.049109646
nan	1.92	1.181	1.039	nan	0.89
1.71	1.617	1.3088	1.36	nan	0.88];


figure
 x1=ISI;
 y1=MFPPR;%
  
s=shadedErrorBar(ISI,nanmean(y1),std(y1)/sqrt(size(y1,1)),'lineprops',{'color', [199 34 22]./255});
s.mainLine.LineWidth = 2;
s.patch.FaceAlpha = 0.5;
ylim([0 2.0])
set(gca, 'YTick', [0:1:2]);
yticklabels({0,'',2})
set(gca, 'XTick', [10 20 50 100]);
xticklabels({10,20,50,100})
set(gca,'TickDir','out');
xlabel('ISI (ms)')
ylabel('PPR')
set(gca,'TickDir','out');


figure
 x2=ISI2
 y2=PFPPR;
s2=shadedErrorBar(ISI2,nanmean(y2),nanstd(y2)/sqrt(size(y2,1)),'lineprops',{'color', [212 170 0]./255});
s2.mainLine.LineWidth = 2;
s2.patch.FaceAlpha = 0.5;
ylim([0 2.0])
set(gca, 'YTick', [0:1:2 ]);
yticklabels({0,'',2})
set(gca, 'XTick', [10 20 50 100 200 500]);
xticklabels({10,20,50,100,200,500})
set(gca,'TickDir','out');
xlabel('ISI (ms)')
ylabel('PPR')
set(gca,'TickDir','out');


%% summary plot of 20 ms ISI amplitudes

PPR20MF=[3.082458295
1.252764733
1.049447978
1.79489
nan
nan
0.898
0.8263
0.9436
nan
nan
2.428216063
nan
];

PPR20PF=[1.654692534
3.361853651
1.58150282
1.151715898
1.731095054
1.736142102
1.626490286
1.92
1.617];


amp2=PPR20PF;

jitt=rand(numel(amp2),1)/2;

figure;

temp=gca

s=scatter(jitt-0.25+ones(size(amp2)),abs(amp2),90'.','filled');
set(gcf,'Position',[2275 63 120 420])
s.MarkerFaceColor=[0 0 0];
s.MarkerFaceAlpha=0.5
Ytick2=temp.YLim(2);

ylim([0 Ytick2]);
set(gca, 'YTick', [0:Ytick2/3:Ytick2 ]);
yticklabels({0,'','',Ytick2})
xlim([0.5 1.5])
set(gca, 'XTick', []);
set(gca,'TickDir','out');
ylabel('PPR (20 ms ISI')
hold on
plot([ 0.75 1.25],[abs(nanmean(amp2)) abs(nanmean(amp2))],'color',[212 170 0]/255,'linewidth',2)
set(gcf,'color','none');
set(gca,'color','none');



% make boxplots
figure
boxplot(PPR20PF, 'symbol','', 'color','k')
ylim([0 3.5])
ylabel('PPR PF 20ms')
set(gcf,'Position',[1240 238 173 480]);

figure
boxplot(PPR20MF, 'symbol','', 'color','k')
ylim([0 3.5])
ylabel('PPR MF 20ms')
set(gcf,'Position',[1240 238 173 480]);

